package com.superbytecode.stencil.strategy.script;

import com.superbytecode.stencil.strategy.SQLScriptStrategy;
import com.superbytecode.stencil.strategy.SQLStrategyEnum;
import com.superbytecode.stencil.entities.table.SQLColumn;
import com.superbytecode.stencil.entities.table.SQLTable;
import com.superbytecode.stencil.utils.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: magician
 * @Package: com.superbytecode.magician.strategy.script
 * @ClassName: PostgreTableScriptService
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/8 16:17
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
@Service
public class PostgreSQLScript implements SQLScriptStrategy, InitializingBean {
    /**
     * Invoked by the containing {@code BeanFactory} after it has set all bean properties
     * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
     * <p>This method allows the bean instance to perform validation of its overall
     * configuration and final initialization when all bean properties have been set.
     *
     * @throws Exception in the event of misconfiguration (such as failure to set an
     *                   essential property) or if initialization fails for any other reason
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        SQLStrategyEnum.sqlScriptStrategyMap.put(SQLStrategyEnum.POSTGRESQL.getCode(), this);
    }
    //------------------------------------------------------------------------------------------------------------------

    /**
     * 获取查询数据库表的SQL脚本
     *
     * @param databaseName  数据库名
     * @param schTableNames 数据表名
     * @return 脚本
     */
    @Override
    public String obtainSQLShowTableScript(String databaseName, String... schTableNames) {
        return "";
    }

    /**
     * 获取数据库表属性列列表
     *
     * @param tableName 数据表名
     * @return 查询表数据脚本
     */
    @Override
    public String obtainSQLShowColumnScript(String tableName) {
        return "";
    }

    /**
     * 构建SQL属性定义
     *
     * @param contentMap 表属性列信息
     *                   <p>数据库不同，包含属性也不同</p>
     * @return 表信息
     */
    @Override
    public SQLColumn obtainSQLColumnDefinition(Map<String, Object> contentMap) {
        return null;
    }

    /**
     * 构建SQL数据表定义
     *
     * @param content 表信息
     *                <p>包含表名、注解</p>
     * @return 表信息
     */
    @Override
    public SQLTable obtainSQLTableDefinition(Map<String, Object> content) {
        SQLTable sqlTable = new SQLTable();
        String name = MapUtil.getString(content, "RELNAME");
        String comment = MapUtil.getString(content, "CMT", name);
        sqlTable.setTableName(name);
        sqlTable.setComments(comment);
        return sqlTable;
    }
}